package com.burgeon.backend.Rest.coupon.xe;
/**
 * 创建积分调整单&购物券 XE
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.Rest.base.sipSign;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class CreateIntegralAndCoupon extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateIntegralAndCoupon(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符
     */
    public String getData(Coupon coupon) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(coupon), url);
        System.err.println(str);
        {
            String code = new JSONArray(str).getJSONObject(0).getString("code");
            String message = new JSONArray(str).getJSONObject(0).getString("message");
            if (!"0".equals(code)) {
                throw new Exception(message);
            }
        }
        {
            String code = new JSONArray(str).getJSONObject(1).getString("code");
            String message = new JSONArray(str).getJSONObject(1).getString("message");
            if (!"0".equals(code)) {
                throw new Exception(message);
            }
        }


        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(Coupon coupon) {


        ArrayList transactions = new ArrayList();

        {
            HashMap params = new HashMap();
            HashMap masterobj = new HashMap(); //头表记录对象
            masterobj.put("table", "WX_VIPINTEGRALADJ");
            masterobj.put("ID", "-1");
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String dateString = sdf.format(now);
            masterobj.put("BILLDATE", dateString);
            masterobj.put("ADJTYPE ", "4");

            masterobj.put("DESCRIPTION", "线上小程序商城积分调整");

            HashMap detailobjs = new HashMap(); //明细表记录对象
            ArrayList list = new ArrayList();
            list.add(4355);

            detailobjs.put("reftables", list);
//******
            ArrayList refobjs = new ArrayList();
            HashMap ref1 = new HashMap();
            ref1.put("table", "WX_VIPINTEGRALADJITEM");//积分调整单单明细
            List addList1 = new ArrayList();
            HashMap map1 = new HashMap();//addlist内容
            map1.put("id", "-1");
            map1.put("C_VIP_ID__CARDNO", coupon.getVipCode());

            map1.put("INTEGRALADJ", -Integer.parseInt(coupon.getIntegral()));

            addList1.add(map1);

            ref1.put("addList", String.valueOf(addList1));

            refobjs.add(ref1);

            detailobjs.put("refobjs", refobjs);

            params.put("masterobj", masterobj);
            params.put("detailobjs", detailobjs);
            params.put("submit", "true");//是否提交
            HashMap transaction = new HashMap();

            transaction.put("command", "ProcessOrder");

            transaction.put("params", params);

            transactions.add(transaction);
        }
//*************************************************************************
        {
            HashMap params = new HashMap();
            params.put("TICKETNO", "'" + coupon.getCode() + "'");//券号
//            if (null!=coupon.getVipIntegral()) {
//                params.put("C_VIPINTEGRALADJ_ID__DOCNO", "'" + coupon.getVipIntegral() + "'");//积分调整单id
//            }
            //params.put("TICKET_TYPE",coupon.getType());//类型

            params.put("C_VIP_ID__CARDNO", "'" + coupon.getVipCode() + "'");
            params.put("CHECKNO", "'" + coupon.getCode() + "'");//校验码
            params.put("PARVALUE", "'" + coupon.getMoney() + "'");//面值
            params.put("DATEBEG", "'" + coupon.getDatebeg() + "'");//起始日期
            params.put("DATEEND", "'" + coupon.getDateend() + "'");//截止日期
            params.put("REMARK", "小程序线上商城");//备注

            params.put("table", "C_V_TDEFTICKET");//表名

            HashMap transaction = new HashMap();

            transaction.put("command", "ObjectCreate");

            transaction.put("params", params);

            transactions.add(transaction);
        }

//*****************************************************************


        sipSign sipSign = new sipSign();
        String sip_timestamp = sipSign.timestamp();
        String sip_sign = sipSign.getMD5Password(account, sip_timestamp, password);

        MultiValueMap Map = new LinkedMultiValueMap();//MultiValueMap可以让一个Key对应多个值
        Map.add("sip_appkey", account);
        Map.add("sip_timestamp", sip_timestamp);
        Map.add("sip_sign", sip_sign);
        Map.add("transactions", String.valueOf(transactions));

        return Map;
    }

    public static void main(String[] args) {
        CreateIntegralAndCoupon c = new CreateIntegralAndCoupon("139.129.93.47:55321", "nea@burgeon.com.cn", "burgeon55321");
        Coupon coupon = new Coupon();
        coupon.setVipCode("17732141036");
        coupon.setIntegral("100");
        coupon.setCode("212dswwxxx");
        coupon.setType("现金券");
        coupon.setCheckno("asdw1we1121");
        coupon.setMoney("123");
        coupon.setDatebeg("20191112");
        coupon.setDateend("20191130");
        // coupon.setVipIntegral("IJ2012100000100");
        try {
            System.err.println(c.getData(coupon));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
